package com.ztgf.shopping.api.cms.audit;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.audit.AuditBO;
import com.ztgf.shopping.models.bo.audit.AuditLogQueryBO;
import com.ztgf.shopping.models.bo.audit.AuditSaveBO;
import com.ztgf.shopping.models.bo.shop.ShopInfoAuditBO;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.dto.audit.AuditDeBangDTO;
import com.ztgf.shopping.models.dto.audit.AuditDeBangVO;
import com.ztgf.shopping.models.service.audit.IAuditDebangService;
import com.ztgf.shopping.models.service.audit.IAuditLogService;
import com.ztgf.shopping.models.service.audit.IAuditService;
import com.ztgf.shopping.models.vo.audit.AuditLogVO;
import com.ztgf.shopping.models.vo.audit.AuditVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/cms/audit")
@Slf4j
@Api(value = "AuditController",tags = "店铺表接口")
public class AuditController {
    @DubboReference
    private IAuditService iAuditService;
    @DubboReference
    private IAuditLogService iAuditLogService;

    @DubboReference
    private IAuditDebangService auditDebangService;

    @ApiOperation(value = "审核列表",notes = "审核列表")
    @PostMapping("/queryPageList")
    public Result<Page<AuditVO>> queryPageList(ShopInfoAuditBO bo){
        Page<AuditVO> page = iAuditService.queryPageList(bo);
        return new Result<>(page);
    }

    @PostMapping("/getInfo")
    @ApiOperation(value = "根据id查找审核详情",notes = "根据id查找审核详情")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<AuditDeBangVO> getInfo(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        AuditDeBangVO vo= new AuditDeBangVO();
        AuditDTO auditDTO = iAuditService.selectById(id);
        AuditDeBangDTO auditDeBangDTO = auditDebangService.selectById(id);
        //vo.setAuditDTO(auditDTO);

        vo.setId(auditDTO.getId());
        vo.setUserId(auditDTO.getUserId());
        vo.setShopId(auditDTO.getShopId());
        vo.setName(auditDTO.getName());
        vo.setShopName(auditDTO.getShopName());
        vo.setParams(auditDTO.getParams());
        vo.setType(auditDTO.getType());
        vo.setStatus(auditDTO.getStatus());
        vo.setContent(auditDTO.getContent());
        vo.setCreateUserName(auditDTO.getCreateUserName());
        vo.setUpdateUserName(auditDTO.getUpdateUserName());
        vo.setCreateTime(auditDTO.getCreateTime());
        vo.setUpdateTime(auditDTO.getUpdateTime());
        //vo.setAuditDeBangDTO(auditDeBangDTO);
        if (auditDeBangDTO!=null){
            vo.setFinanceName(auditDeBangDTO.getFinanceName());
            vo.setFinancePhone(auditDeBangDTO.getFinancePhone());
            vo.setFinanceWx(auditDeBangDTO.getFinanceWx());
            vo.setFinanceQq(auditDeBangDTO.getFinanceQq());
            vo.setSendName(auditDeBangDTO.getSendName());
            vo.setSendPhone(auditDeBangDTO.getSendPhone());
            vo.setSendWx(auditDeBangDTO.getSendWx());
            vo.setSendQq(auditDeBangDTO.getSendQq());
        }

        if (vo == null){
            return new Result<>(CodeEnum.FAIL);
        }else {
            return new Result<>(vo);
        }
    }

    @PostMapping("/getNum")
    @ApiOperation(value = "根据类型获取审核数量",notes = "根据类型获取审核数量")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<Integer> getNum(Integer type){
        if(type == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        Integer number = iAuditService.getNum(type);
        return new Result<>(number);
    }



    @ApiOperation(value = "保存审核信息",notes = "保存审核信息")
    @PostMapping("/save")
    public Result save(AuditSaveBO bo){
        return iAuditService.save(bo);
    }


    @ApiOperation(value = "审核日志列表",notes = "审核日志列表")
    @PostMapping("/queryLogPageList")
    public Result<Page<AuditLogVO>> queryLogPageList(AuditLogQueryBO bo){
        Page<AuditLogVO> page = iAuditLogService.queryLogPageList(bo);
        return new Result<>(page);
    }

    @ApiOperation(value = "获取审核列表",notes = "获取审核列表")
    @PostMapping("/queryPageListNew")
    public Result<Page<AuditVO>> queryPageListNew(AuditBO bo){
        Page<AuditVO> page = iAuditService.queryPageListNew(bo);
        if(page.getRecords() != null && page.getRecords().size() > 0){
            for (AuditVO vo : page.getRecords()){
                List<AuditLogVO> logVOList = iAuditLogService.queryList(vo.getId());
                vo.setLogVOList(logVOList);
            }
        }

        return new Result<>(page);
    }

    @ApiOperation(value = "根据id获取审核日志信息",notes = "根据id获取审核日志信息")
    @PostMapping("/getLogVO")
    public Result<AuditLogVO> getLogVO(Long id){
        AuditLogVO vo = iAuditLogService.get(id);
        return new Result(vo);
    }

    @ApiOperation(value = "根据id获取审核记录",notes = "根据id获取审核记录")
    @PostMapping("/get")
    public Result<AuditVO> get(Long id){
        return iAuditService.get(id);
    }

    @ApiOperation(value = "根据AuditId获取审核记录列表",notes = "根据AuditId获取审核记录列表")
    @PostMapping("/getLogsByAuditId")
    public Result<List<AuditLogVO>> getLogsByAuditId(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<AuditLogVO> logVOList = iAuditLogService.queryList(id);
        return new Result(logVOList);
    }
}
